package io.renren.modules.job.task;

import io.renren.modules.weixin.Service.AccessTokenService;
import io.renren.modules.weixin.Util.MessageUtil;
import io.renren.modules.weixin.entity.AccessToken;
import io.renren.modules.weixin.message.resp.TextMessage;
import io.renren.modules.weixin.message.sendall.Filter;
import io.renren.modules.weixin.message.sendall.Send;
import io.renren.modules.weixin.message.sendall.Text;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

import static io.renren.modules.weixin.Util.WeixinUtil.httpRequest;

@Component("testWeiXin")
public class TestWeiXin {
    private Logger logger = LoggerFactory.getLogger(getClass());


    @Autowired
    private AccessTokenService accessTokenService;

    public final static String sendall_url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN";
    public void test(){
        int result = 0;
        AccessToken count = accessTokenService.countToken();

        String requestUrl = sendall_url.replace("ACCESS_TOKEN", count.getToken());

        Send s = new Send();
        Filter f = new Filter();
        Text t = new Text();
        f.setIs_to_all(true);
        t.setContent("CONTENT");
        s.setMsgtype("text");
        s.setText(t);
        s.setFilter(f);

        // 将菜单对象转换成json字符串
        String jsonMenu = JSONObject.fromObject(s).toString();
        // 调用接口创建菜单
        JSONObject jsonObject = httpRequest(requestUrl, "POST", jsonMenu);

        if (null != jsonObject) {
            if (0 != jsonObject.getInt("errcode")) {
                result = jsonObject.getInt("errcode");
                logger.error("创建菜单失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
            }
        }

    }


    public void test2(){
        logger.info("我是不带参数的test2方法，正在被执行");
    }

}
